<script lang="ts">
	import Vue from "vue";
	export default Vue.extend({
		mpType: "app",
		onLaunch() {
			// #ifndef H5 || APP-PLUS
			this.checkVersion();
			// #endif

			// #ifdef APP-PLUS
			this.uniPush();
			// #endif
		},
		onPageNotFound() {
			uni.redirectTo({ url: "/pages/common/notFound" });
		},
		methods: {
			uniPush() {
				plus.push.setAutoNotification(true);

				// 点击消息事件处理
				plus.push.addEventListener("click", (payload) => {
					uni.switchTab({ url: "/pages/tabs/messages" });
				});
				// 接收消息事件处理 (透传消息才可触发)
				plus.push.addEventListener("receive", (res: any) => {
					uni.$emit("receiveNotice", res.payload);

					const innerAudioContext = uni.createInnerAudioContext();
					innerAudioContext.src = res.payload;
					innerAudioContext.play();

					innerAudioContext.onPlay(res => console.log(res));
					innerAudioContext.onError((res) =>
						uni.showToast({ title: JSON.stringify(res), icon: "none" })
					);
					innerAudioContext.onEnded(innerAudioContext.destroy);
				});
			},
			checkVersion() {
				const {
					onCheckForUpdate,
					onUpdateReady,
					onUpdateFailed,
					applyUpdate,
				} = uni.getUpdateManager();

				onCheckForUpdate((res) => {
					/* 请求完新版本信息的回调 */
				});

				onUpdateReady((res) => {
					uni.showModal({
						title: "更新提示",
						content: "新版本已经准备好，是否重启应用？",
						// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
						success: (res) => res.confirm && applyUpdate(),
					});
				});

				onUpdateFailed((res) => {
					// 新的版本下载失败
					uni.showToast({
						title: "新版本下载失败，请稍后再试。",
						icon: "none",
					});
				});
			},
		},
	});
</script>
